var uploadImage = require('../../utils/uploadFile.js');

Page({
  data: {
    files: [{
      url: 'http://zbyerpdata.oss-cn-hangzhou.aliyuncs.com/2019/12/10/1575969675358581574.jpg',
    },{
        url: 'http://zbyerpdata.oss-cn-hangzhou.aliyuncs.com/2019/12/10/1575947300529560971.jpg'
    }]
  },
  onLoad() {
    this.setData({
      selectFile: this.selectFile.bind(this),
      uplaodFile: this.uplaodFile.bind(this)
    })
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log('ww',res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片

      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  selectFile(files) {
    console.log('files', files)
    // 返回false可以阻止某次文件上传
  },
  uplaodFile(files) {
    console.log('测试')
    var that = this;
    return new Promise((resolve, reject) => {
    for (var file of files.tempFilePaths){
      console.log('测试', file)
      wx.compressImage({
        src: file, // 图片路径
        quality: 10,
         // 压缩质量
        success: function(res){
        console.log('压缩',res)
          uploadImage(res.tempFilePath,function (result) {
          console.log("======上传成功图片地址为：", result, that.data.files);
          var arr = [];
          arr[0] = {url: result};
          var tempImgPaths = that.data.files.concat(arr);
          that.setData({
            files: tempImgPaths
          })
          wx.hideLoading();
        }, function (result) {
          console.log("======上传失败======", result);
          wx.hideLoading()
        })
        }
      })
    }
    })
  },
  uploadError(e) {
    console.log('upload error', e.detail)
  },
  uploadSuccess(e) {
    console.log('upload success', e.detail)
  }
});
